﻿require(["./config"], function() {
	require(["jquery"], function($) {
		function zhuce(){
				this.circle=$(".bottom .circle");
				this.move();
				this.check();
				this.submit();
				this.code=null;
				this.yanzheng=null;
				this.repeat=null;
			}
			zhuce.prototype={
				constructor:zhuce,
				move(){
					this.circle.on("mousedown",(event)=>{
						let x1=event.offsetX;
//						console.log(x1);
						let x2=$(".bottom .top").offset().left;
//						console.log(x2);
						$(document).on("mousemove",(event)=>{
							event.preventDefault();
							let x3=event.pageX;
							let x=x3-x2-x1;
							if(x<0)
							{
								x=0;
							}
							if(x>230)
							{
								x=230;
							}
							this.circle.css("left",x);
							
						});
						$(document).on("mouseup",()=>{
							$(document).off("mousemove mouseup");
//							console.log(this.circle.css("left"));
							if(this.circle.css("left")==230+"px")
							{
								
								alert("验证成功");
								this.circle.off("mousedown");
								this.yanzheng=true;
							}
						})
					})
				},
				check(){
					
					$(".in1").on("blur",()=>{
//						console.log($(".in1").val())
						$.get("http://localhost/api/check.php?username="+$(".in1").val(), (res) => {
//							console.log(res);
							var r=JSON.parse(res);
							console.log(r);
							$(".s1").text(r.data);
							this.code=r.code;
							console.log(this.code);
							
						});
					})
				},
				submit(){
					let sub=$(".main>.center>.right>.btn");
//					console.log(sub.text());
					sub.on("click",()=>{
						let v1=$(".mima1").val();
						let v2=$(".mima2").val();
						console.log(v1==v2);
						console.log(this.code);
						console.log(this.yanzheng);
						if(v1==v2&&this.code==0&&this.yanzheng)
						{
							$.post("http://localhost/api/register.php",
							{
								username:$(".in1").val(),
								password:v1
							},(res)=>{
								res=JSON.parse(res);
								console.log(res);
								console.log($(".in1").val());
								console.log(v1);
								alert("注册成功");
								location="/html/login.html";
							});
							
							
						}
						else if(v1!==v2)
						{
							
								alert("两次密码输入不同");
							
						}
						else if(this.code!=0)
						{
							alert("用户名已被占用")
						}
					})
					
					
				}
			}
			new zhuce();
	})
})